package com.firesrc.service.client;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.firesrc.util.EncryptUtil;
import com.firesrc.util.SystemConfigConstant;

public abstract class SuperClientTask implements Runnable {
  @Override
  public void run() {
    String data = getSendDataString();
    if (isEmptyRst(data)) {
      return;
    }
    postData(data);
  }

  protected abstract boolean isEmptyRst(String rstString);

  protected abstract String getSendDataString();

  protected abstract String getOpenatorType();

  protected abstract void handleRespString(String text);

  public void postData(String data) {
    try {
      Document post = Jsoup.connect(getServerAddress()).data(SystemConfigConstant.TRAFFER_DATA_FLAG, EncryptUtil.encryptString(data))
          .data(SystemConfigConstant.TRAFFER_TYPE_FLAG, EncryptUtil.encryptString(getOpenatorType())).timeout(60000).post();
      handleRespString(EncryptUtil.dencryptString(post.text()));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private String getServerAddress() {
    //    return "http://192.168.1.228:8080/ProcessData";
    return "http://paiming.xicp.net:8080//ProcessData";
    //    return "http://localhost:8080/ProcessData";
  }
}